package display
{
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	/**
	 * 位图解析类
	 * @author gcxk721
	 * 2012-6-21
	 * 下午2:13:31
	 */
	public class QhBitmapAnalysiser
	{
		
		public function QhBitmapAnalysiser()
		{
			
		}
		
		public static function analysisByXYFY(bytes : ByteArray) : Vector.<Vector.<Vector.<QhBitmap>>>
		{
			var vec : Vector.<Vector.<Vector.<QhBitmap>>> = new Vector.<Vector.<Vector.<QhBitmap>>>(22);
			bytes.uncompress();
			var len : int = bytes.readInt();
			var info : ByteArray = new ByteArray();
			bytes.readBytes(info, 0, len);
			var lst : Vector.<ByteArray> = analysisBitmap(bytes);
			var index : int;
			
			while(info.bytesAvailable != 0)
			{
				var action : int = info.readByte();
				if(vec[action] == null)
				{
					vec[action] = new Vector.<Vector.<QhBitmap>>(8);
				}
				for (var i:int = 0; i < 5; i++) 
				{
					var dir : int = info.readByte();
					if(vec[action][dir] == null)
					{
						vec[action][dir] = new Vector.<QhBitmap>();
						if(dir == 1 || dir == 2 || dir == 3)//需要翻转
						{
							vec[action][8 - dir] = new Vector.<QhBitmap>();
						}
					}
					len = info.readByte();
					for (var j:int = 0; j < len; j++) 
					{
						var qhBitmap : QhBitmap = new QhBitmap();
						qhBitmap.x = int(info.readDouble());
						qhBitmap.y = int(info.readDouble());
						qhBitmap.bmdWidth = info.readShort();
						qhBitmap.bmdHeight = info.readShort();
						vec[action][dir].push(qhBitmap);
						
						if(dir == 1 || dir == 2 || dir == 3)//需要翻转
						{
							var matrixBitmap : QhBitmap = new QhBitmap();
							matrixBitmap.y = qhBitmap.y;
							matrixBitmap.bmdWidth = qhBitmap.bmdWidth;
							matrixBitmap.bmdHeight = qhBitmap.bmdHeight;
							qhBitmap.setMatrix(matrixBitmap);
							vec[action][8 - dir].push(matrixBitmap);
						}
						index  = info.readShort();
						lst[index].position = 0;
						qhBitmap.analysis(lst[index]);
					}
				}
			}
			return vec;
		}
		
		/**
		 * 
		 * @param	bytes
		 * @return
		 */
		public static function analysisBitmap(bytes : ByteArray) : Vector.<ByteArray>
		{
			var vec : Vector.<ByteArray> = new Vector.<ByteArray>();
			var len : int;
			var btBytes : ByteArray;
			while(bytes.bytesAvailable != 0)
			{
				len = bytes.readInt();
				btBytes = new ByteArray();
				bytes.readBytes(btBytes, 0, len);
				btBytes.position = 0;
				vec.push(btBytes);
			}
			return vec;
		}
		
		/**
		 * 解析 
		 * @param bytes
		 * 
		 */
		public static function analysis(bytes : ByteArray) : Vector.<Vector.<Vector.<QhBitmap>>>
		{
			var vec : Vector.<Vector.<Vector.<QhBitmap>>> = new Vector.<Vector.<Vector.<QhBitmap>>>(22);
			var frame : int;
			var len : int;
			var secondBytes : ByteArray;
			while(bytes.bytesAvailable != 0)
			{
				frame = bytes.readByte();
				len = bytes.readInt();
				secondBytes = new ByteArray;
				bytes.readBytes(secondBytes, 0 ,len);
				vec[frame] = secondAnalysis(secondBytes);
			}
			return vec;
		}
		
		private static function secondAnalysis(bytes : ByteArray) : Vector.<Vector.<QhBitmap>>
		{
			var vec : Vector.<Vector.<QhBitmap>> = new Vector.<Vector.<QhBitmap>>(8);
			var num : int = bytes.readByte();
			vec[0] = new Vector.<QhBitmap>(num);//0
			num = bytes.readByte();
			vec[1] = new Vector.<QhBitmap>(num);//1
			vec[7] = new Vector.<QhBitmap>(num);//7
			num = bytes.readByte();
			vec[2] = new Vector.<QhBitmap>(num);//2
			vec[6] = new Vector.<QhBitmap>(num);//6
			num = bytes.readByte();
			vec[3] = new Vector.<QhBitmap>(num);
			vec[5] = new Vector.<QhBitmap>(num);
			num = bytes.readByte();
			vec[4] = new Vector.<QhBitmap>(num);
			
			var len : int;
			var thirdBytes : ByteArray;
			while(bytes.bytesAvailable != 0)
			{
				len = bytes.readInt();
				thirdBytes = new ByteArray();
				bytes.readBytes(thirdBytes, 0, len);
				thirdAnalysis(thirdBytes, vec);
			}
			return vec;
		}
		
		private static function thirdAnalysis(bytes : ByteArray, vec : Vector.<Vector.<QhBitmap>>) : void
		{
			var qhBitmap : QhBitmap = new QhBitmap();
			var dir : int = bytes.readByte();
			var frame : int = bytes.readByte();
			qhBitmap.x = int(bytes.readDouble());
			qhBitmap.y = int(bytes.readDouble());
			qhBitmap.bmdWidth = bytes.readShort();
			qhBitmap.bmdHeight = bytes.readShort();
			if(dir == 1 || dir == 2 || dir == 3)//需要翻转
			{
				var matrixBitmap : QhBitmap = new QhBitmap();
//				matrixBitmap.x = qhBitmap.x;
				matrixBitmap.y = qhBitmap.y;
				matrixBitmap.bmdWidth = qhBitmap.bmdWidth;
				matrixBitmap.bmdHeight = qhBitmap.bmdHeight;
				qhBitmap.setMatrix(matrixBitmap);
				vec[8 - dir][frame] = matrixBitmap;
			}
			qhBitmap.analysis(bytes);
			vec[dir][frame] = qhBitmap;
		}
		
		private static var matrix : Matrix = new Matrix(-1 , 0, 0, 1, 0, 0);
		static public function cacheMatrixBitmap(bitmapData : BitmapData) : BitmapData 
		{
			var bitData : BitmapData = new BitmapData(bitmapData.width, bitmapData.height, true, 0);
			matrix.tx = bitmapData.width;
			bitData.draw(bitmapData, matrix);
			return bitData;
		}
	}
}